import { createStore } from 'vuex'
import createPersistedState from "vuex-persistedstate"

export default createStore({
  state: {
    phone: null,
    password: null,
    loginUser: null,
    // 购物车状态
    shoppingCar: [],
    //准备购买的东西
    readyCar:[],
    //一个订单
    oneorder:{},

    // 收藏
    collec:[]
  },
  getters: {
    getNum(state) {
      return state.phone ? state.phone : "登录/注册"
    }
  },
  mutations: {
    login(state, data) {
      state.phone = data.phone._value
      state.password = data.password._value
    },
    
    //加入购物车
    jsonCar(state, data) {
      let aa=state.shoppingCar.findIndex(item=>item.bannerData.key == data.bannerData.key)
      // console.log(aa);
      if(aa == -1){
        state.shoppingCar.push(data)

      }else{
        // state.shoppingCar.bannerData.num+=1
        console.log(state.shoppingCar);
      }
    },
    
    //删除手机
    exit(state){
      delete state.phone
    },
    //删除里面的内容
    del(state){
      let aa=state.shoppingCar.filter(item=>{
        // console.log(item.bannerData.check);
       return item.bannerData.check == false
      })
      state.shoppingCar=aa
      // console.log(aa);
    },

    //准备购买,确认订单
    readyCarData(state){
      //1.当点击确认后调用这个方法
      // console.log("1");
      //2.首先筛选原本打勾的赋值给readyCar
      state.readyCar=state.shoppingCar.filter(item=>{
        return item.bannerData.check ==true
      })
      //3.其次筛选原本的界面没打勾的，赋值给shoppingCar()
      // state.shoppingCar=state.shoppingCar.filter(item=>{
      //   return item.bannerData.check ==false
      // })
    },

    //立即购买
    thisbuynow(state,item){
      // console.log("立即购买的==>",item);
      // console.log(state.readyCar);
      state.readyCar=[]
      state.readyCar.push(item)
    },
    
    //点击取消type=1
    deltype(state,time){
      let id = JSON.parse(window.localStorage.getItem("id"))//取出id值
      if(id==null){
        //若为空直接保存
        let index = 10
        window.localStorage.setItem("id",JSON.stringify(index))
        state.oneorder["id"]=index//id
      }else{
        //否则
        let index = id +1
        window.localStorage.setItem("id",JSON.stringify(index))
        state.oneorder["id"]=index//id

      }
      //取出总价格
      let totalmoney = JSON.parse(window.localStorage.getItem("allmoney"))
      //订单信息
      state.oneorder["lists"]=state.readyCar
      state.oneorder["type"]=1//类型
      state.oneorder["total"]=totalmoney//价格
      state.oneorder["time"]=time//时间
      //购物车筛选出没打勾的
      state.shoppingCar=state.shoppingCar.filter(item=>{
        return item.bannerData.check == false
      })
    },
    buynow(state,time){
      let id = JSON.parse(window.localStorage.getItem("id"))//取出id值
      if(id==null){
        //若为空直接保存
        let index = 10
        window.localStorage.setItem("id",JSON.stringify(index))
        state.oneorder["id"]=index//id
      }else{
        //否则
        let index = id +1
        window.localStorage.setItem("id",JSON.stringify(index))
        state.oneorder["id"]=index//id

      }
      //取出总价格
      let totalmoney = JSON.parse(window.localStorage.getItem("allmoney"))
      //订单信息
      state.oneorder["lists"]=state.readyCar
      state.oneorder["type"]=2
      state.oneorder["total"]=totalmoney
      state.oneorder["time"]=time
      //购物车筛选出没打勾的
      state.shoppingCar=state.shoppingCar.filter(item=>{
        return item.bannerData.check == false
      })
    },

    // 收藏
    collecDel(state,data){
      state.collec.push(data)
      // console.log(state.collec);
      
    },
    // 收藏删除
    dels(state,id){
     let aa = state.collec.findIndex((item)=>{
      return item.bannerData.key == id
     })
     state.collec.splice(aa,1)
    }

    
  },
  actions: {

  },
  plugins: [createPersistedState({
    storage: window.sessionStorage  // 同localStorage相同，只是将vuex的所有值存储到sessionStorage中
  })],

  modules: {
  }
})
